import { result } from 'lodash';
import { Component, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { CreateOrEditBopModalComponent } from '@app/admin/bop/create-or-edit-bop-modal.component';
import { Paginator } from 'primeng/components/paginator/paginator';
import { BopUnifyServiceProxy } from '@shared/service-proxies/service-proxies';
import { BopNodeServiceProxy, TechProcessServiceProxy } from '@shared/service-proxies/service-proxies';
import { Table } from 'primeng/components/table/table';
import { finalize } from 'rxjs/operators';
import { InputSwitchModule } from 'primeng/inputswitch';
import { SearchComponent } from './search/search.component';
import { FileManageDetailComponent } from './file-manage-detail/file-manage-detail.component';
import { MessageService } from '@abp/message/message.service';

@Component({
    selector: 'app-file-manage',
    templateUrl: './file-manage.component.html',
    styleUrls: ['./file-manage.component.less']
})
export class FileManageComponent extends AppComponentBase {
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('inputSwitch') inputSwitch: InputSwitchModule;
    @ViewChild('searchMsg') searchMsg: SearchComponent;
    @ViewChild('fileManageDetail') manageDetail: FileManageDetailComponent;
    //子组件传参
    rowdata = {};
    //启用停用
    use = undefined
    //型号下拉数据
    Models = []
    //搜索时的model值
    modelValue = undefined
    //图号下拉数据
    drawingNo = []
    //搜索时的model值
    drawingNoValue = undefined
    //弹窗文件编号下拉数据
    textCode = []
    textCodeValue = undefined
    //表格文件编号下拉数据
    textCodesValue = undefined
    saveFlag = true;
    constructor(
        injector: Injector,
        private _qualityPlatformServiceProxy: BopUnifyServiceProxy,
        private _accountServiceProxy: BopNodeServiceProxy,
        private _messageService: MessageService,
        private _techProcessServiceProxy: TechProcessServiceProxy,
    ) {
        super(injector);
    }
    copy(row) {
        this.message.confirm(
            '确认复制这条规程吗',
            (isConfirmed) => {
                if (isConfirmed) {
                    abp.ui.setBusy();
                    this._qualityPlatformServiceProxy.copyTechtechnique(row.id)
                        .pipe(finalize(() => { })).subscribe(result => {
                            abp.ui.clearBusy();
                            if (result) {
                                this._messageService.success('操作成功!');
                                this.getTableList();
                            } else {
                                this._messageService.error('已存在复制完成的工艺规程，请销毁后在执行此操作!');
                            }
                        })
                }
            }
        );
      
    }
    // 获取表格
    getTableList(event?: LazyLoadEvent) {
        // if (this.primengTableHelper.shouldResetPaging(event)) {
        //     this.paginator.changePage(0);
        //     return;
        // }
        abp.ui.setBusy();
        this._qualityPlatformServiceProxy.getBopUnifyList(
            this.textCodesValue,
            this.drawingNoValue,
            this.modelValue,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            abp.ui.clearBusy();
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();
        });
    }
    //获取图号
    getModelList() {
        this.drawingNo = [];
        this._qualityPlatformServiceProxy.getModuleCodeList(undefined)
            .pipe(finalize(() => { })).subscribe(result => {
                result.forEach(item => {
                    this.drawingNo.push({
                        label: item,
                        value: item
                    })
                })
            })
    }
    //获取型号
    getdrawingList() {
        this.Models = [];
        this._qualityPlatformServiceProxy.getProductCodeList(undefined)
            .pipe(finalize(() => { })).subscribe(result => {
                result.forEach(item => {
                    this.Models.push({
                        label: item,
                        value: item
                    })
                })
            })
    }
    //获取文件编号
    getTextCode() {
        this.textCode = [];
        this._qualityPlatformServiceProxy.getTechniqueNumberList(undefined)
            .pipe(finalize(() => { })).subscribe(result => {
                result.forEach(item => {
                    this.textCode.push({
                        label: item,
                        value: item
                    })
                })
            })
    }
    // 重置
    reset() {
        this.modelValue = undefined;
        this.drawingNoValue = undefined;
        this.textCodesValue = undefined;
        this.textCodeValue = undefined;
        this.getModelList();
        this.getdrawingList();
        this.getTextCode();
        // this.getTableList();
    }
    // 搜索
    Search() {
        if (this.textCodeValue) {
            this.rowdata = {};
            abp.ui.setBusy(undefined, '', 1);
            this._techProcessServiceProxy.realGetTechDocsByPps(
                this.textCodeValue,
            ).pipe(finalize(() => { })).subscribe(result => {
                abp.ui.clearBusy();
                this.rowdata = result;
                setTimeout(() => {
                    this.searchMsg.show();
                }, 20)
            });
        } else {
            this._messageService.error("请输入文件编号！")
        }

    }
    look(row) {
        let that = this
        this.rowdata = row;
        setTimeout(() => {
            that.manageDetail.show();
            that.manageDetail.getTreeInfo();
        }, 20);
    }
    // 删除
    delete(row): void {
        this.message.confirm(
            '确认删除这条数据吗',
            (isConfirmed) => {
                if (isConfirmed) {
                    if (this.saveFlag) {
                        this.saveFlag = false;
                        this._accountServiceProxy.deleteTechniqueById(row.id).pipe(finalize(() => { }))
                            .subscribe(result => {
                                this.saveFlag = true;
                                if (result.code == 0) {
                                    this._messageService.success('删除成功！');
                                    this.getModelList();
                                    this.getdrawingList();
                                    this.getTextCode();
                                    this.getTableList();
                                } else {
                                    this._messageService.error(result.message)
                                }
                            })
                    }
                }
            }
        );
    }
    // 启用停用
    statusChange(row) {

    }
    ngOnInit() {
        this.getModelList();
        this.getdrawingList();
        this.getTextCode();
    }

}
